package com.javaear.zblog4java.system.controller;

import com.alibaba.fastjson.JSON;
import com.javaear.generalmapper.GeneralMapper;
import com.javaear.zblog4java.system.component.AppConfig;
import com.javaear.zblog4java.system.entity.dto.SettingDTO;
import com.javaear.zblog4java.system.entity.model.ConfigModel;
import com.javaear.zblog4java.system.util.RequestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("system")
public class SettingController {

    @Autowired
    private GeneralMapper generalMapper;

    private ConfigModel settingConfig;

    @RequestMapping(value = "setting", method = RequestMethod.GET)
    public void viewSetting(ModelMap modelMap) {
        modelMap.put("settingDTO", JSON.parseObject((settingConfig = generalMapper.selectOne(new ConfigModel("settingDTO"))).getContent(), SettingDTO.class));
        modelMap.put("title", "网站设置");
    }

    @RequestMapping(value = "setting", method = RequestMethod.POST)
    public void saveSetting(HttpServletRequest request, ModelMap modelMap) {
        SettingDTO settingDTO = RequestUtils.copyProperties(request, SettingDTO.class);
        settingConfig.setContent(JSON.toJSONString(settingDTO));
        generalMapper.updateSelectiveById(settingConfig);
        modelMap.put("settingDTO", settingDTO);
        modelMap.put("title", "网站设置");
        AppConfig.loadSetting();
    }
}
